//
//  MyinfoViewController.m
//  TimeMovie
//
//  Created by qianfeng on 15/11/30.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "MyinfoViewController.h"
#import "FirstTableViewCell.h"
#import "SecondTableViewCell.h"
#import "ThirdTableViewCell.h"
#import "QRCodeController.h"
@interface MyinfoViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    //表格视图
    UITableView* _tableView;
    //记录表格的滚动
    CGFloat contentOffsetY;
    
    CGFloat newContentOffsetY;
    //记录导航条的透明度
    CGFloat navAlpha;
}
@end

@implementation MyinfoViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //隐藏导航栏用自定制的
    self.navigationController.navigationBarHidden=YES;
    //小信箱  以下两个方法调用顺序不能颠倒  保证自定制的导航条在最上层
    [self createTableView];
    [self createSmallEmail];
}
#pragma mark - 创建top视图
-(UIView*)createnavTopView{
    //top的背景图
    UIImageView* topView=[[UIImageView alloc]initWithFrame:CGRectMake(0,0, screen_WIDTH, 160.0f*scale_Screen)];
    topView.userInteractionEnabled=YES;
    topView.image=[UIImage imageNamed:@"icon_myinfo_top"];
    //头像
    UIImageView* iconView=[[UIImageView alloc]initWithFrame:CGRectMake(30, 60, 55.0f*scale_Screen, 55.0f*scale_Screen)];
    iconView.image=[UIImage imageNamed:@"icon_avatar_default_image_55x55"];
    //后期这里加手势
    [topView addSubview:iconView];
    //登陆注册按钮
    NSArray* backArray=@[@"bt_solid_orange_68",@"v10_skip_bt_gray"];
    NSArray* titleArray=@[@"登录",@"注册"];
    for (int i=0; i<2; i++) {
        UIButton* button=[MYCustomView createButtonWithFrame:CGRectMake(120+(100.0f*scale_Screen)*i, 70.0f, 70.0f*scale_Screen, 30*scale_Screen) target:self SEL:@selector(resingAndLogin:) backgoundImage:[UIImage imageNamed:backArray[i]] title:titleArray[i] image:nil];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font=fontSize14;
        [topView addSubview:button];
    }
    return topView;
}
#pragma mark - 小信箱
-(void)createSmallEmail{
    //使用自定制的导航栏
    [self createNavViewWithAlpha:0];
    //加上小信箱 后期这里加手势
    UIImageView* smallView=[[UIImageView alloc]initWithFrame:CGRectMake(320*scale_Screen, 30, 25*scale_Screen, 20*scale_Screen)];
    smallView.userInteractionEnabled=YES;
    smallView.image=[UIImage imageNamed:@"icon_my_message"];
    [self.navView addSubview:smallView];
}
#pragma mark -改变导航条的透明度
-(void)changeNavAlpha{
self.navView.backgroundColor=[UIColor colorWithRed:11 / 255.0f green:95 / 255.0f blue:183 / 255.0f alpha:navAlpha/255.0f];
}
#pragma mark - 创建TableView
-(void)createTableView{
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0, screen_WIDTH, screen_HEIGHT-tabBar_Height) style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    self.automaticallyAdjustsScrollViewInsets=NO;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tableView.tableHeaderView=[self createnavTopView];
    _tableView.bounces=NO;
    [self.view addSubview:_tableView];
}
#pragma mark - 实现登陆注册按钮的方法
-(void)resingAndLogin:(UIButton*)button
{

}
#pragma mark - 表格协议
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
        {
            return 1;
        }
            break;
        case 1:
        {
            return 3;
        }
            break;
        case 2:
        {
            return 5;
        }
            break;
        default:
            break;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
         return 125.0f *scale_Screen;
    }else{
        return 45.0f * scale_Screen;
    }
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
        {
            FirstTableViewCell* cell=(FirstTableViewCell*)[self createCellWithID:@"cell1"tableView:tableView andClassName:@"FirstTableViewCell"];
        return cell;
        }
            break;
        case 1:
        {
            SecondTableViewCell* cell=(SecondTableViewCell*)[self createCellWithID:@"cell2" tableView:tableView andClassName:@"SecondTableViewCell"];
            NSArray* imageArray=@[@"icon_my_coupon",@"icon_my_movie",@"icon_my_collection"];
            NSArray* titleArray=@[@"我的优惠券",@"我的电影",@"我的收藏"];
            
            if (indexPath.row==0) {
                cell.leftImage.image=[UIImage imageNamed:imageArray[indexPath.row]];
                cell.titleLabel.text=titleArray[indexPath.row];
            }
            if (indexPath.row==1) {
                cell.leftImage.image=[UIImage imageNamed:imageArray[indexPath.row]];
                cell.titleLabel.text=titleArray[indexPath.row];
            }
            if (indexPath.row==2) {
                cell.leftImage.image=[UIImage imageNamed:imageArray[indexPath.row]];
                cell.titleLabel.text=titleArray[indexPath.row];
            }
            return cell;
        }
            break;
        case 2:
        {
            NSArray* titleArray=@[@"设置",@"扫描二维码",@"意见反馈",@"喜欢我们,去打分",@"商城使用帮助"];
            ThirdTableViewCell* cell=(ThirdTableViewCell*)[self createCellWithID:@"cell3" tableView:tableView andClassName:@"ThirdTableViewCell"];
            for (int i=0; i<titleArray.count; i++) {
                if (indexPath.row==i) {
                    cell.titleLabel.text=titleArray[i];
                }
            }
            return cell;
        }
            break;
        default:
            break;
    }
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{

    return 20.0f;
}
#pragma mark - 表格选中方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //扫描二维码
    if (indexPath.section==2&&indexPath.row==1) {
        QRCodeController* qrVc=[[QRCodeController alloc]init];
        [self presentViewController:qrVc animated:YES completion:^{
            //后期传值
        }];
    }

}
#pragma 创建小表格的封装方法
- (UITableViewCell *)createCellWithID:(NSString *)cellID tableView:(UITableView *)tableView andClassName:(NSString *)className{
    
    UITableViewCell *cell;
    if (cellID) {
        cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    }
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:className owner:self options:nil] lastObject];
    }
    return cell;
}
#pragma mark - 判断表格的滚动方向
//将要开始拖动
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    contentOffsetY =scrollView.contentOffset.y;
}
//滚动中
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    newContentOffsetY=scrollView.contentOffset.y;
    //向上滚动
    if (newContentOffsetY>contentOffsetY) {
        navAlpha=newContentOffsetY;
        if (navAlpha>=255.000000) {
            navAlpha=255.0f;
        }
        //向上滑动到头了
        if (newContentOffsetY+_tableView.frame.size.height==scrollView.contentSize.height) {
            if (newContentOffsetY<255.000000) {
                navAlpha=255.0f;
            }
        }
        [self changeNavAlpha];
    }else if (newContentOffsetY < contentOffsetY){
        //向下滚动
        navAlpha=newContentOffsetY;
        [self changeNavAlpha];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
